package com.syezon.wifikey.cengwang.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.syezon.wifikey.R;

import java.util.Random;

public class CengwangLoadingView extends FrameLayout {

    private Context mContext;
    private ImageView mIvInnerCircle;
    private ImageView mIvExtCircle;
    private TextView mTvProgress;

    private Random mRandom = new Random();
    private int progress;

    public CengwangLoadingView(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public CengwangLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.app_cengwang_layout_loading, this, true);
        mIvExtCircle = (ImageView) findViewById(R.id.iv_ext_circle);
        mIvExtCircle.setAnimation(getExtCircleAnim());
        mIvInnerCircle = (ImageView) findViewById(R.id.iv_inner_circle);
        mIvInnerCircle.setAnimation(getInnerCircleAnim());
        mTvProgress = (TextView) findViewById(R.id.tv_progress);
    }

    private Animation getExtCircleAnim() {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1500);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        return rotateAnimation;
    }

    private Animation getInnerCircleAnim() {
        RotateAnimation rotateAnimation = new RotateAnimation(0, -360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1500);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        return rotateAnimation;
    }

    public void setProgress(int progress) {
        if (progress >= 99) {
            progress = 99;
        }
        this.progress = progress;
        mTvProgress.setText(this.progress + "");
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Animation animation = mIvExtCircle.getAnimation() == null ? getExtCircleAnim() : mIvExtCircle.getAnimation();
        mIvExtCircle.startAnimation(animation);
        Animation animation1 = mIvInnerCircle.getAnimation() == null ? getInnerCircleAnim() : mIvInnerCircle.getAnimation();
        mIvInnerCircle.startAnimation(animation1);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                setProgress(0);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setProgress(0);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                setProgress(progress + mRandom.nextInt(40));
            }
        });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mIvExtCircle.clearAnimation();
        mIvInnerCircle.clearAnimation();
        setProgress(0);
    }
}
